//
//  YMBookCaseView.swift
//  YMSwiftUI
//
//  Created by anscen on 2022/3/23.


import SwiftUI
import Moya

enum YMHomeCaseState :String {
    case grid = "九宫格"
    case list = "表格"
}
struct YMBookCaseView: View {
    @State private var width :CGFloat = grid_width
    @State var listData :[YMBook] = []
    @State var listAdap :[GridItem] = [GridItem(.adaptive(minimum: grid_width, maximum: grid_width + 15), spacing: 0, alignment: .center)]
    @State var state    :YMHomeCaseState = .grid
    var body: some View {
        GeometryReader { rootSize in
            VStack {
                ScrollView(.vertical, showsIndicators: false) {
                    LazyVGrid(columns: listAdap, spacing: 0){
                        ForEach(listData, id: \.self){
                            item in
                            if self.state == .grid{
                                YMBookCaseCell(book: item,width:width)
                            }else{
                                YMListCell(book: item,width: rootSize.size.width)
                            }
                        }
                    }
                }.onAppear(perform: {
                    width = (rootSize.size.width - 60)/3 - 0.1
                    if self.state == .grid{
                        self.state = .list
                        self.listAdap = [GridItem(.adaptive(minimum: list_width, maximum: list_height), spacing: 0, alignment: .center)]
                    }else{
                        self.state = .grid
                        self.listAdap = [GridItem(.adaptive(minimum: width, maximum: width + 15), spacing: 0, alignment: .center)]
                    }
                    debugPrint("==================")
                    debugPrint(rootSize.size.width)
                }).navigationBarItems(leading:NavigationLink(destination: {
                    YMSearchView()
                }, label: {
                    Image("icon_home_search").frame(width: 44, height: 44, alignment: .leading)
                }),trailing: Button(action: {
                    if self.state == .grid{
                        self.state = .list
                        self.listAdap = [GridItem(.adaptive(minimum: list_width, maximum: list_height), spacing: 0, alignment: .center)]
                    }else{
                        self.state = .grid
                        self.listAdap = [GridItem(.adaptive(minimum: (rootSize.size.width - 60)/3 - 0.1, maximum: (rootSize.size.width - 60)/3 - 0.1 + 15), spacing: 0, alignment: .center)]
                    }
                }, label: {
                    Text(state.rawValue).padding(EdgeInsets(top: 5, leading: 10, bottom: 5, trailing: 10)).foregroundColor(Color.appx333333()).font(.system(size: 16)).lineLimit(1).overlay(RoundedRectangle(cornerRadius: 5, style: .continuous).stroke(AppXDDDDDD, lineWidth: 1))
                }))
            }.onAppear {
                loadData()
            }
        }
    }
    func loadData(){
        shelfQueue.getData(page: 1, size: 20) { datas in
            self.listData = datas
        }
    }
}
struct YMBookCaseCell : View{
    @State var book :YMBook
    @State var isActvie :Bool = false
    @State var width :CGFloat = grid_width
    var body: some View{
        VStack {
            NavigationLink(isActive: $isActvie) {
                YMDetailView(bookId: book.bookId ?? "")
            } label: {

                VStack(alignment: .center, spacing: 10) {
                    ZStack(alignment: .topLeading) {
                        WebImage(url: URL(string: book.cover?.cover ?? "")).resizable().placeholder {
                            RoundedRectangle(cornerRadius:5).foregroundColor(.gray)
                        }.indicator(.activity).transition(.fade).cornerRadius(5).frame(width: width, height: width * 1.35, alignment: .center).padding(EdgeInsets.init(top: 15, leading: 0, bottom: 0, trailing: 0))
                        if book.minorCate.count > 0{
                            Text(book.minorCate).padding(EdgeInsets(top: 3, leading: 6, bottom: 3, trailing: 6)).background(Color(white: 0.65)).foregroundColor(.white).font(.system(size: 12)).lineLimit(1).overlay(RoundedRectangle(cornerRadius: 3, style: .continuous).stroke(.white, lineWidth: 1)).offset(x: 10, y: 25)
                            
                        }
                    }
                    Text(book.title ?? "").foregroundColor(Color.appx000000()).font(Font(UIFont.systemFont(ofSize: 17, weight: .medium))).lineLimit(1)
                    Text(book.author ?? "").foregroundColor(Color.appx666666()).padding(EdgeInsets(top:0, leading: 0, bottom: 10, trailing: 0)).font(.system(size: 14)).lineLimit(1)
                }
            }
        }
    }
}
